package org.kev.transit.worker;

import java.util.ArrayList;
import org.kev.transit.data.Stop;
import org.kev.transit.util.TransitUtil;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * 
 * @author kevin
 *
 */
public class StopUpdateWorker implements Runnable {
	private static final String TAG = "StopUpdateWorker";
	private Activity mParent = null;
	private ArrayList <Stop> mList = null;

	/*private String mRouteTypeId=null;
	private String mRouteId=null;
	private String mDirectionId=null;*/
	
	/**
	 * 
	 * @param parent
	 * @param list
	 */
	public StopUpdateWorker(Activity parent, ArrayList <Stop> list)
	{
		mParent=parent;
		mList=list;
	}
	/**
	 * 
	 */
	public void run() {
		processLookupTable();
	}
	/**
	 * 
	 */
	private void processLookupTable(){
		Log.d(TAG,">>>processLookupTable");
		if(mList ==null||mParent==null){
			Log.e(TAG,"Null parms="+mList+mParent);
			return;
		}
		int count = mList.size();
		String where ="";

		Cursor cursor=null;		
		for(int i=0;i<count;i++){
			Stop s = mList.get(i);
			where = Stop.ROUTE_TYPE + "=? AND "+  
					Stop.ROUTE + "=? AND " +
					Stop.DIRECTION + "= ? AND "+
					Stop.STOP_ID+ "= ? ";
			Log.d(TAG,"wheres="+where);
			String args[]={s.routeTypeId,s.routeId,s.directionId,s.id};
			Log.d(TAG,"args="+TransitUtil.dump(args));


			try{
				cursor = mParent.managedQuery(Stop.CONTENT_URI, new String[] 
				     {Stop.STOP_ID},where,args,null);
				//update lookup table
				Log.d(TAG,"found in db #"+cursor.count());
				
				if(cursor.count()==0){
					insertStop(s);
				}else{
					updateStop(s);
				}
			}catch(Exception e){
				Log.e(TAG,"UNKNOWN error will be ignored",e);
			}finally{
				if(cursor!=null)
					cursor.close();
			}
		}
		Log.d(TAG,"<<< processLookupTable");
	}

	/**
	 * 
	 * @param data
	 * @return row count
	 */
	private Uri insertStop(Stop data){
		Log.d(TAG,">>> insertStop"+mParent+data);
		if (mParent ==null || data==null){
			return null;
		}

		Log.d(TAG, "+++insertStop");
		Log.d(TAG, data.toXml() );
		ContentValues values = new ContentValues();
		values.put(Stop.STOP_ID, data.id);
		values.put(Stop.TITLE, data.title);
		values.put(Stop.ROUTE_TYPE, data.routeTypeId);
		values.put(Stop.ROUTE, data.routeId);		
		values.put(Stop.DIRECTION, data.directionId);
		values.put(Stop.LAT, data.lat);
		values.put(Stop.LON, data.lon);
		values.put(Stop.CREATE_DATE,new  Long(System.currentTimeMillis() ) );
		Uri curl = mParent.getContentResolver().insert(Stop.CONTENT_URI,values);
		//done with lookup now work on timeTable
			Log.d(TAG,"<<< insertStop"+curl);

		return curl;
	}

	/**
	 * 
	 * @param data
	 * @return row count
	 */
	private int updateStop(Stop data){
		if (mParent ==null|| data==null){
			return 0;
		}
		Log.d(TAG, "+++updateStop");
		Log.d(TAG, data.toXml() );
		ContentValues values = new ContentValues();
		values.put(Stop.STOP_ID, data.id);
		values.put(Stop.ROUTE_TYPE, data.routeTypeId);
		values.put(Stop.ROUTE, data.routeId);		
		values.put(Stop.DIRECTION, data.directionId);
		values.put(Stop.TITLE, data.title);
		values.put(Stop.LAT, data.lat);
		values.put(Stop.LON, data.lon);
		values.put(Stop.TITLE,data.title);
		values.put(Stop.CREATE_DATE,new  Long(System.currentTimeMillis() ) );
		String where = Stop.STOP_ID+"= ? AND "+Stop.ROUTE_TYPE+"=? AND "+
						Stop.ROUTE+"=? AND "+Stop.DIRECTION+"=?";
		String[] args={data.id,data.routeTypeId,data.routeId,data.directionId};



		int row = mParent.getContentResolver().update(Stop.CONTENT_URI,values,where,args);
		Log.d(TAG,"row="+row );
		return row;
	}


}
